//
//  HMEmoticonKeyboardView.swift
//  CZWeibo31
//
//  Created by apple on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        // 添加控件
        addSubview(emoticonBottomView)
        addSubview(emoticonPageView)
        addSubview(pageControl)
        // 添加约束
        emoticonBottomView.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        
        emoticonPageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emoticonBottomView.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(emoticonPageView).offset(5)
            make.centerX.equalTo(emoticonPageView)
        }
        
        // 底部视图闭包回调
        emoticonBottomView.closure = {(type: HMEmoticonBottomViewType)->() in
            // 获取indexPath
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            // 滚动到指定位置
            self.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            // 设置pageControl
            self.setupPageControl(indexPath)
        }
        
        setupPageControl(NSIndexPath(forItem: 0, inSection: 0))
    }
    // MARK: - 懒加载控件
    // 底部视图
    private lazy var emoticonBottomView: HMEmoticonBottomView = HMEmoticonBottomView(frame: CGRectZero)
    // 指定表情键盘视图
    private lazy var emoticonPageView: HMEmoticonPageView = {
        let view = HMEmoticonPageView()
        view.backgroundColor = self.backgroundColor
        // 分页
        view.pagingEnabled = true
        // 取消弹簧效果
        view.bounces = false
        // 隐藏滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        // 设置代理
        view.delegate = self
        return view
    }()
    // pageControl
    private lazy var pageControl: UIPageControl = {
        let view = UIPageControl()
        // 默认颜色
//        view.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        // 只用kvc 给只读属性赋值 (xcode 6.4 以后 就不在开放了 但是可以使用 前提 你的知道他)
        view.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        // 选中的颜色
//        view.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        view.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        // 取消用户交互
        view.userInteractionEnabled = false
        return view
    }()

}

extension HMEmoticonKeyboardView: UICollectionViewDelegate {
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 获取屏幕中心点x (相对于pageView)
        let centerX = SCREENW*0.5 + scrollView.contentOffset.x
        
        // 获取屏幕显示的cellz (由于系统没有帮我们排序 需要我们自己排序)
        let cells = self.emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        // 如果屏幕上显示两个cell 我才会判断
        if cells.count == 2 {
            // 第一个cell
            let firstCell = cells.first!
            // 最后一个cell
            let lastCell = cells.last!
            // 定义一个indexPath
            let indexPath: NSIndexPath
            // 判断中心点 是否在firstCell的frame中
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                indexPath = emoticonPageView.indexPathForCell(firstCell)!
            }else {
                indexPath = emoticonPageView.indexPathForCell(lastCell)!
            }
            // 设置选中的按钮
            emoticonBottomView.setupCurrentButton(indexPath.section)
            
            setupPageControl(indexPath)
            
        }
        
    }
    // 设置pageControl
    func setupPageControl(indexPath: NSIndexPath) {
        // 总页数
        pageControl.numberOfPages = HMEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
        // 选中的页数
        pageControl.currentPage = indexPath.item
    }
}



